package edu.scut.autoProcessor.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import edu.scut.autoProcessor.po.FaultPo;

public class FaultDao extends AbstractDao{

	private static final Log logger = LogFactory.getLog(FaultDao.class);
	
	private static final int PAGE_LENGTH = 15;
	
	public static void insertFault(FaultPo po){
		SqlSession sqlSession = factory.openSession();
		
		try{
			sqlSession.insert("alertProcessor.faultMapper.insertFault", po);
		}catch(Exception e){
			logger.error(e.getMessage());
		}finally{
			sqlSession.close();
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<FaultPo> queryFault(Map parameters, int page){
		SqlSession sqlSession = factory.openSession();
		
		try{
			int offset = (page - 1) * PAGE_LENGTH;
			
			parameters.put("offset", offset);
			parameters.put("pageLength", PAGE_LENGTH);
			
			return sqlSession.selectList("alertProcessor.faultMapper.selectFault", parameters);
		}catch(Exception e){
			logger.error(e.getMessage());
			return null;
		}finally{
			sqlSession.close();
		}
	}
}
